<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<html>
<head><title>Change Screen</title>
</head>
<body>
<center>
<%
	class XSSchecker
	{
		public String sanitize(String string) 
		{
			string.replaceAll("(?i)<script.*?>.*?</script.*?>", "");   	
			string.replaceAll("(?i)<.*?javascript:.*?>.*?</.*?>", ""); 
			string.replaceAll("(?i)<.*?\\s+on.*?>.*?</.*?>", "");
			return string;
		}
	};

       try
       {
		XSSchecker checker = new XSSchecker();

		if(session.getAttribute("logged_in") == "true")
		{
			out.println("<p><h2>Logged In</p><h2>");
			if(session.getAttribute("user_level") == "administrator")
			{
				if(checker.sanitize(request.getParameter("presentation_id")) == "" || checker.sanitize(request.getParameter("screen_id")) == "")
				{
					out.println("You must fill in both the screen id and the presentation id<br>");
				}
				else
				{
					out.println("<br>You selected presentation id: " + checker.sanitize(request.getParameter("presentation_id")) + "<br>");
					out.println("<br>You selected screen id: " + checker.sanitize(request.getParameter("screen_id")) + "<br>");

// before the presentation can be deleted from the screen:
// the presentation must be on that screen
	
					String DRIVER = "com.mysql.jdbc.Driver";
					Class.forName(DRIVER);

					Connection con=null;
					ResultSet rst=null;
					PreparedStatement stmt=null;

					String url="jdbc:mysql://ecstiger.cs.andrews.edu/d562_2010_01?user=u562_2010_01&password=YPJ8f4We";
					con=DriverManager.getConnection(url);

					stmt = con.prepareStatement("DELETE FROM screen_presentations WHERE screen_id = ? AND presentation_id= ? ;");
					stmt.setString(1, checker.sanitize(request.getParameter("screen_id"))); 
					stmt.setString(2, checker.sanitize(request.getParameter("presentation_id"))); 
					int Result = stmt.executeUpdate();
			
					if(Result==1)
					{
						out.println("<br>Delete Succesful<br>");
					}
					else
					{
						out.println("<br>Delete Unsuccesful<br>");
					}		

					stmt.close();
					con.close();
				}
			}
			else
			{
				if(checker.sanitize(request.getParameter("presentation_id")) == "" || checker.sanitize(request.getParameter("screen_id")) == "")
				{
					out.println("You must fill in both the screen id and the presentation id<br>");
				}
				else
				{
					out.println("<br>You selected presentation id: " + checker.sanitize(request.getParameter("presentation_id")) + "<br>");
					out.println("<br>You selected screen id: " + checker.sanitize(request.getParameter("screen_id")) + "<br>");

// before the presentation can be deleted from the screen:
// the presentation must be on that screen
// the user must have control of that screen
// and the user must own that presentation

					String DRIVER = "com.mysql.jdbc.Driver";
					Class.forName(DRIVER);
	
					Connection con=null;
					ResultSet rst=null;
					PreparedStatement stmt=null;

					String url="jdbc:mysql://ecstiger.cs.andrews.edu/d562_2010_01?user=u562_2010_01&password=YPJ8f4We";
					con=DriverManager.getConnection(url);

					stmt = con.prepareStatement("SELECT * FROM screen_presentations, screen_owners, presentation WHERE screen_presentations.screen_id= ? AND screen_presentations.presentation_id = ? AND screen_owners.user_id = ? AND screen_owners.screen_id= ? AND presentation.user_id= ? ;");
					stmt.setString(1, checker.sanitize(request.getParameter("screen_id"))); 
					stmt.setString(2, checker.sanitize(request.getParameter("presentation_id")));
					stmt.setString(3, (String)session.getAttribute("id"));
					stmt.setString(4, checker.sanitize(request.getParameter("screen_id"))); 
					stmt.setString(5, (String)session.getAttribute("id")); 

					rst = stmt.executeQuery();

					if(rst.next())
					{
						stmt = con.prepareStatement("DELETE FROM screen_presentations WHERE screen_id = ? AND presentation_id= ? ;");
						stmt.setString(1, checker.sanitize(request.getParameter("screen_id"))); 
						stmt.setString(2, checker.sanitize(request.getParameter("presentation_id")));

						int Result=stmt.executeUpdate();	

						if(Result==1)
						{
							out.println("<br>Delete Succesful<br>");
						}
						else
						{
							out.println("<br>Delete Unsuccesful<br>");
						}
					}
					
					rst.close();
					stmt.close();
					con.close();
				}
			}
		}
		else
		{
			out.println("Not Logged In");
		}
       }
       catch(Exception e)
       {
           out.println(e);
       }	
%>
<br><a href="menu.jsp">Main Menu</a>
</center>
</body>
</table>
</center>
</div>


</body>
</html>
